package top.cross.webapp.common.webview;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import top.cross.webapp.common.util.RSAUtils;
import top.cross.webapp.common.util.WebviewMethodUtil;
import top.crossrun.webview.bridge.core.pls.BridgeCallBackFunction;
import top.crossrun.webview.bridge.core.pls.WebViewBridgePlugin;
import top.crossrun.webview.bridge.webview.x5.X5WebViewActivity;

public class TestX5Activity extends X5WebViewActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initIntentData() {
        Intent data = new Intent();
        data.putExtra("url", "file:///android_asset/views/start.html");
        setIntent(data);
        super.initIntentData();
    }

    @Override
    public void initPlugin() {
        super.initPlugin();
//        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                try {
                    JSONObject json = new JSONObject(new JSONObject(msg).getString("params"));
                    String var1 = json.getString("key");
                    SharedPreferences sp = getSharedPreferences(var1, 0);
                    var1 = sp.getString(var1, "");
                    JSONObject var5;
                    (var5 = new JSONObject()).put("value", var1);
                    JSONObject var4;
                    (var4 = new JSONObject()).put("data", var5);
                    callBackFunction.onCallBackFunction(var4.toString());
                } catch (Exception var3) {
                    Toast.makeText(TestX5Activity.this, "参数错误！1", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public String getPluginName() {
                return "getSpference";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                try {
                    JSONObject json = new JSONObject(new JSONObject(msg).getString("params"));
                    String var1 = json.getString("key");
                    String var3 = json.getString("value");
                    SharedPreferences sp = getSharedPreferences(var1, 0);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString(var1, var3);
                    editor.apply();
                    callBackFunction.onCallBackFunction("successful");
                } catch (Exception var4) {
                    Toast.makeText(TestX5Activity.this, "参数错误！2", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public String getPluginName() {
                return "setSpference";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                String id = Settings.System.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
                callBackFunction.onCallBackFunction(id);
            }

            @Override
            public String getPluginName() {
                return "getUUID";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                callBackFunction.onCallBackFunction("");
            }

            @Override
            public String getPluginName() {
                return "getClientId";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                Log.e("console", "zhyw.checkH5Zip" + msg);
            }

            @Override
            public String getPluginName() {
                return "zhyw.checkH5Zip";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
            }

            @Override
            public String getPluginName() {
                return "getEncryptKey";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            public String[] DEFAUL_IMPORT = new String[]{"com.ustc.mobile.NewGongDanChuLiNew"
                    , "com.ustc.mobile.NewGongDanChuLi"
                    , "com.ustcinfo.ict.zqktNew"
                    , "com.tydic.SalePhone"
                    , "com.kdgc.bluetoothprinter"
                    , "com.kinggrid.iappphotos"
            };

            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                Log.e("console", "getPackageInfobyname=" + msg);
                try {
                    JSONObject jsonObject = new JSONObject(msg);
                    JSONArray packageArray = jsonObject.getJSONArray("params");
                    JSONArray callbackArr = new JSONArray();
                    for (int i = 0; i < packageArray.length(); i++) {
                        if (isDefault((String) packageArray.get(i))) {
                            JSONObject jo = new JSONObject();
                            jo.put("isinstall", true);
                            jo.put("versionName", "0.0.0");
                            jo.put("packageName", packageArray.get(i));
                            callbackArr.put(jo.toString());
                        } else {
                            callbackArr.put(WebviewMethodUtil.judgePackageInstall(getContext()
                                    , packageArray.get(i) + ""));
                        }
                    }
                    callBackFunction.onCallBackFunction(callbackArr.toString());
                } catch (Exception e) {

                }
            }

            public boolean isDefault(String pkg) {
                for (String s : DEFAUL_IMPORT) {
                    if (s.equals(pkg)) {
                        return true;
                    }
                }
                return false;
            }

            @Override
            public String getPluginName() {
                return "getPackageInfobyname";
            }
        });

        bridgeHelper.pluginRegister(new WebViewBridgePlugin() {
            @Override
            public void handlerBridgeMsg(String msg, BridgeCallBackFunction callBackFunction) {
                try {
                    PackageInfo var5 = getPackageManager().getPackageInfo(getPackageName(), 0);
                    JSONObject var6 = new JSONObject();
                    JSONObject var3;
                    (var3 = new JSONObject()).put("versioncode", var5.versionCode);
                    var3.put("versionName", var5.versionName);
                    var6.put("data", var3);
                    callBackFunction.onCallBackFunction(var6.toString());
                } catch (Exception var4) {
                    var4.printStackTrace();
                    Toast.makeText(TestX5Activity.this, "版本信息获取失败!", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public String getPluginName() {
                return "getPackageInfo";
            }
        });
    }
}
